﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
using QLKH.PopUp.Khachhang;

namespace QLKH.View.Khachhang
{
    public partial class TTXuly : UserControl
    {
        DataView view = new DataView();
        public TTXuly()
        {
            InitializeComponent();
        }

        private void TTXuly_Load(object sender, EventArgs e)
        {
            clKhachhang clkh = new clKhachhang();
            DataTable dtkh = clkh.getvipham();

            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtkh.DefaultView;
        }

        private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            lbrowindex.Text = e.RowIndex.ToString();
        }

        private void tool_capnhat_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int64 id = Int64.Parse(dataGrid.Rows[row].Cells["idvp"].Value.ToString());
                clVipham clvp = new clVipham();
                if (clvp.updatestatus(id, Int16.Parse("0")) > 0)
                {
                    clKhachhang clkh = new clKhachhang();
                    DataTable dtkh = clkh.getvipham();

                    dataGrid.AutoGenerateColumns = false;
                    dataGrid.DataSource = view = dtkh.DefaultView;
                }
            }
            else
                MessageBox.Show("Bạn chưa chọn khách hàng.");
        }

        public void TimKiem(string txtsearch, string type, Int16 id)
        {
            string querySearch = "1=1";
            if (txtsearch.Length > 0)
                querySearch += " and (tenTK LIKE '%" + txtsearch + "%')";
            if (id > 0)
            {
                if (type == "D")
                    querySearch += " and idDuong = " + id + " and idDHTong = 0";
                else
                    querySearch += " and idDHTong = " + id;
            }
            view.RowFilter = querySearch;
        }

        private void tool_search_Click(object sender, EventArgs e)
        {
            popTimkiem usfritem = new popTimkiem();
            // ké thằng GetValue của form1 vào form2
            usfritem.MyGetData = new popTimkiem.TimKiem(TimKiem);
            usfritem.ShowDialog();
        }
    }
}
